<%@ page language="java" contentType="text/html; charset=UTF-8"  isELIgnored="false"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="../../css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="../../js/jquery-2.1.4.js"></script>
<script src="../../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../../js/path.js"></script>
<style>
	td {
		text-align: center;
		width:50%;
	}
	
	#container {
		width: 70%;
		margin-left: 200px;
	}
	
	#jieguo1,#jieguo2 {
		color: red;
	}
	
	.close{
		float: right;
	}
	
	#updatePage {
		width: 50%;
	}
	button{
		background-color: greenyellow;
	}
</style>
</head>

<body>
	<table border="1 " cellspacing="0 " align="center" class='updateTable'>
					
		<tr>
			<td colspan="2" align="center">
				电脑信息修改
				<input type="button" value="点此关闭" style="font-family: '微软雅黑';font-size: 5px; background-color: red;" onclick='closeUpPage();' class='close'/>
			</td>
			
		</tr>
		
		<tr>
			<td>电脑品牌</td>
			<td>
				<input type="text"  name="brand"  onblur="yzName();"/>
				<div id="jieguo1"></div>
			</td>
		</tr>
		<tr>
			<td>电脑型号</td>
			<td >
				<input type="text" name="model" onblur="yzModel();"/>
				<div id="jieguo3"></div>
			</td>
		</tr>
		<tr>
			<td>电脑价格</td>
			<td >
				<input type="text" name="price" onblur="yzSalary();"/>
				<div id="jieguo2"></div>
			</td>
		</tr>
		<tr>
			<td>所属公司</td>
			<td>
				<select class="ownCompany" >
					
				</select>
				
				
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="button" id="but1" value="修改 " style="background-color: gold"/>
				
			</td>
		</tr>
	</table>
	<div id='jieguo'></div>
	<script type="text/javascript">
		//得到银行卡的ID
		var id = ${param.id};
		function yzName(){
			var cardNo = $('input[name="brand"]').val();
			if(""==cardNo ||  null == cardNo){
				$('#jieguo1').text("请重新输入...");
				return false;
			}else{
				$('#jieguo1').empty();
				return true;
			}
		}
		function yzModel(){
			var cardNo = $('input[name="model"]').val();
			if(""==cardNo ||  null == cardNo){
				$('#jieguo3').text("请重新输入...");
				return false;
			}else{
				$('#jieguo3').empty();
				return true;
			}
		}
		function yzSalary(){
			var cardNo = $('input[name="price"]').val();
			var patten = new RegExp(/^\d{1,8}(?:\.\d{0,2})?$/);
			if(patten.test(cardNo)!=true){
			 	$('#jieguo2').text("您输入的数字有误 ，请重新输入...");
			 	return false;
			 }else {
			 	$('#jieguo2').empty();
			 	return true;
			 }
			 
		}
		//修改方法开始
		$('#but1').click(function(){
			//先验证数据的正确性
			//验证输入的卡号和余额
			if(yzName() && yzSalary()){
				//正确时在进行修改
				
				var ont = $('.ownCompany')[0].value;
				var brand=$('input[name=brand]').val();
				var model = $('input[name=model]').val();
				var price = $('input[name=price]').val();
				$.ajax({
					data:{
					
					'id':id,
					'brand':brand,
					'companyId':ont,
					'model':model,
					'price':price
				
					},
					url:getPath()+"/computer/updateById",
					type:'post',
					dataType:'json',
					success:function(jsonData){
						if(jsonData.result=='success'){
							window.location.href="computer-list.html";
						}else{
							$('#jieguo').text(jsonData.jieguo);
						}
					}
					
				})
			}
			
			//在进行修改
		});
		
		
		//所有的学历和教师进行操作
		$(function(){
			
			//alert(id);
			
			//得到所有 学历
			$.ajax({
				
				url:getPath()+"/company/findAllCompanys",
				type:'post',
				dataType:'json',
				success:function(jsonData){
					var data = jsonData.data;
					$(data).each(function(index,data){
						var name = data.name;
						var id = data.id;
						var op = $('<option name="ownCompany" value="'+id+'">'+name+'</option>')
						$('.ownCompany').append(op);
					})
				}
			
			});
			
			//2 根据 id找到银行卡id=5的这条记录的所有信息 CardEntity findById(int id) ==>bankId=11
			//2拿到对应的所有信息开始
			$.ajax({
				data:{
					
					'id':id,
				},
				url:getPath()+"/computer/findComputerById",
				type:'post',
				dataType:'json',
				success:function(jsonData){
					
					console.log(jsonData);
					
					//给卡号赋值
					var id = jsonData.data.id;
					$('input[name="brand"]').val(jsonData.data.brand);
					$('input[name="model"]').val(jsonData.data.model);
					$('input[name="price"]').val(jsonData.data.price);
					
					var companyId = jsonData.data.companyId;
					
					var companyName = jsonData.data.companyName;
					//3选中对应的学历   教师
					
					//4. 遍历所有学历选中对应的学历
					var ops = $('.ownCompany').find('option');
					//console.log(ops.length);
					ops.each(function(index,data){
						console.log(data);
						var bankIdVal = data.value;
						if(bankIdVal==companyId){
							$(data).attr('selected','selected');
						}
					})
					
				}
			})
			
		});
	</script>
</body>
</html>